﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using C.Unite;
using C.Entities;
using ChanExt;
using MySoft.Data;
using System.Text;
using LitJson;


namespace ArtLand.OA.Project.PublicModel
{
    public partial class SelectUsers : CommonPageBase
    {
        List<OA_Department> mentList = TbsDbSession.Default.From<OA_Department>().ToList();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTree();
                BindGrid(GetWhere());
            }
        }

        #region BindGrid
        private void BindGrid(WhereClip wc)
        {
            Grid1.SelectedRowIndex = -1;
            // 1.设置总项数
            Grid1.RecordCount = TbsDbSession.Default.From<HR_EMP>().Where(wc).Count();

            List<HR_EMP> items = TbsDbSession.Default.From<HR_EMP>().Where(wc).OrderBy(HR_EMP._.ID.Desc).Page(Grid1.PageSize, (Grid1.PageIndex + 1)).ToList();



            // 3.绑定到Grid
            Grid1.DataSource = items;
            Grid1.DataBind();
        }



        protected void Grid1_PageIndexChange(object sender, ChanExt.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid(GetWhere());
        }


        #endregion

        #region Events

        public WhereClip GetWhere()
        {
            WhereClip wc = WhereClip.All;
            if (!String.IsNullOrEmpty(ttbxMyBox2.Text))
            {
                wc = wc && (HR_EMP._.EMNAME.Contain(ttbxMyBox2.Text) || HR_EMP._.EMPLID.Contain(ttbxMyBox2.Text));
            }
            if (treeMenu.SelectedNodeID != "" && treeMenu.SelectedNodeID != "1")
            {
                OA_Department dep = TbsDbSession.Default.From<OA_Department>().Where(OA_Department._.DepID == int.Parse(treeMenu.SelectedNodeID)).ToFirst();
                wc = wc && (HR_EMP._.DepPath.Contain(dep.DepPath + dep.DepID + "|"));
            }
            return wc;
        }



        // 点击 TwinTriggerBox 的搜索按钮
        protected void ttbxMyBox2_Trigger2Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(ttbxMyBox2.Text))
            {
                // 执行搜索动作
                BindGrid(GetWhere());
                ttbxMyBox2.ShowTrigger1 = true;
            }
            else
            {
                Alert.ShowInTop("请输入你要搜索的关键词！");
            }
        }

        // 点击 TwinTriggerBox 的取消按钮
        protected void ttbxMyBox2_Trigger1Click(object sender, EventArgs e)
        {
            // 执行清空动作

            ttbxMyBox2.Text = "";
            ttbxMyBox2.ShowTrigger1 = false;
            BindGrid(GetWhere());
        }


        #endregion


        private void BindTree()
        {
            List<OA_Department> items = mentList.FindAll(delegate(OA_Department tas) { return tas.TopID == 0; });// TbsDbSession.Default.From<GS_ArticleSort>().Where(GS_ArticleSort._.FatherID == 0).OrderBy(GS_ArticleSort._.ShowNo.Asc).ToList();
            foreach (OA_Department row in items)
            {
                TreeNode node = new TreeNode();
                node.Text = row.DepName;
                node.NodeID = row.DepID.ToString();
                node.AutoPostBack = true;
                node.EnablePostBack = true;
                node.Expanded = true;
                treeMenu.Nodes.Add(node);
                ResolveSubTree(row.DepID, node);
            }
        }

        private void ResolveSubTree(int id, TreeNode treeNode)
        {
            List<OA_Department> items = mentList.FindAll(delegate(OA_Department tas) { return tas.TopID == id; }); //TbsDbSession.Default.From<GS_ArticleSort>().Where(GS_ArticleSort._.FatherID == id).OrderBy(GS_ArticleSort._.ShowNo.Asc).ToList();
            if (items.Count > 0)
            {
                foreach (OA_Department row in items)
                {
                    TreeNode node = new TreeNode();
                    node.Text = row.DepName;
                    node.NodeID = row.DepID.ToString();
                    node.AutoPostBack = true;
                    node.EnablePostBack = true;
                    node.Expanded = true;
                    treeNode.Nodes.Add(node);
                    ResolveSubTree(row.DepID, node);
                }
            }

        }

        protected void treeMenu_NodeCommand(object sender, TreeCommandEventArgs e)
        {
            BindGrid(GetWhere());
        }

        protected void Grid1_RowClick(object sender, GridRowClickEventArgs e)
        {

            //txt_SelectUsers.Text = "123";
        }

        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            if(hid_IDList.Text=="")
                txt_SelectUsers.Text = "";
            string nowSelect = txt_SelectUsers.Text;
            nowSelect = nowSelect.Replace("您已选择：", "");
            string nowIDSelect = "";
            nowIDSelect = hid_IDList.Text;
            System.Web.UI.WebControls.CheckBox dl = (System.Web.UI.WebControls.CheckBox)Grid1.Rows[Grid1.SelectedRowIndexArray[0]].FindControl("CheckBox1");
            bool checkedState = dl.Checked;
            string id = Grid1.Rows[Grid1.SelectedRowIndexArray[0]].Values[1].ToString();
            string name = Grid1.Rows[Grid1.SelectedRowIndexArray[0]].Values[3].ToString();
            if (checkedState)
            {
                List<string> list = StringHelper.GetSubStringList(nowIDSelect, ',');
                if (list.Count > 0)
                {
                    if (!string.IsNullOrEmpty(Request["single"]))
                    {
                        dl.Checked = false;
                        Alert.Show("您只能选择一项！");
                    }
                    else
                    {
                        if (!nowIDSelect.Contains("," + id + ","))
                        {
                            nowIDSelect += "," + id + ",";
                            nowSelect += name + "，";
                        }
                    }
                }
                else
                {
                    if (!nowIDSelect.Contains("," + id + ","))
                    {
                        nowIDSelect += "," + id + ",";
                        nowSelect += name + "，";
                    }
                }
            }
            else
            {
                if (nowIDSelect.Contains("," + id + ","))
                {
                    nowIDSelect = nowIDSelect.Replace("," + id + ",", "");
                    nowSelect = nowSelect.Replace(name + "，", "");
                }
            }

            if (nowIDSelect != "")
            {
                txt_SelectUsers.Text = "您已选择：" + nowSelect;
                hid_IDList.Text = nowIDSelect;
            }
            else
            {
                txt_SelectUsers.Text = "";
                hid_IDList.Text = "";
            }
        }

        protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
        {
            System.Web.UI.WebControls.CheckBox dl = (System.Web.UI.WebControls.CheckBox)Grid1.Rows[e.RowIndex].FindControl("CheckBox1");
            string id = e.Values[1].ToString();
            if (hid_IDList.Text.Contains("," + id + ","))
            {
                dl.Checked = true;
            }
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            string selecttxt = txt_SelectUsers.Text.Trim();
            if (selecttxt != "")
            {
                selecttxt = selecttxt.Replace("您已选择：", "");
                selecttxt = selecttxt.Remove(selecttxt.Length - 1, 1);
                PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference(hid_IDList.Text, selecttxt) + ActiveWindow.GetHideReference());
            }
            else
                Alert.Show("请选择一项进行再提交！");
        }

        protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e)
        {

        }
    }
}